package com.sdlogan.pbudget;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.graphics.Color;
import android.view.View;

/**
 * The class that will create bar graphs with the budget display information
 * @author Sean
 *
 */
public class BarGraph {
		double [] dataset1;
		double [] dataset2;
		String time;
		MoneyTable mt;
		List<dbPackage> source;
		String month;
		String year;
		Iterator it;
		dbPackage current;
	
		/**
		 *  Constructor for the chart
		 * @param time : Value that tells whether to create a yearly graph or a monthly
		 * @param list : The list containing the budget information
		 */
		
		public BarGraph(String time,List<dbPackage> list) {
			this.time = time;
			Calendar cal = Calendar.getInstance();
			year = Integer.toString(cal.get(Calendar.YEAR),10);
			Date date = new Date();
			month = new SimpleDateFormat("MMMM").format(date);
			source = list;
			updateValues();
		}

		/**
		 * Update the two arrays to be used by the graphical display
		 */
		private void updateValues() {
			double monthvalue = 0.0;
			double yearvalue = 0.0;
			if (time.equals("Monthly")) { 
				dataset1 = new double[31];
				dataset2 = new double[31];
				for (int counter1 = 0; counter1 < 31; counter1++) {
					dataset1[counter1] = 0.0;
					dataset2[counter1] = 0.0;
						it = source.iterator();
						while (it.hasNext()) {
							current = (dbPackage) it.next();
								if (current.type.equals("Income") && Integer.parseInt(current.day) == counter1) {
									dataset1[Integer.parseInt(current.day) - 1] += current.monthlyvalue;
								}
								else if (current.type.equals("Expense") && Integer.parseInt(current.day) == counter1){ //Expense
									dataset2[Integer.parseInt(current.day) - 1] += current.monthlyvalue;
								}
								else {
									//do nothing
								}
						}
					}
				}
			else {
				dataset1 = new double[12];
				dataset2 = new double[12];
				for (int counter2 = 0; counter2 < 12; counter2++) {
					dataset1[counter2] = 0.0;
					dataset2[counter2] = 0.0;
				}
						it = source.iterator();
						while (it.hasNext()) {
							current = (dbPackage) it.next();
							
							if (current.type.equals("Income") && month.equals(current.month)) {
								dataset1[getMonth(current.month)] += current.monthlyvalue;
							}
							else if (current.type.equals("Expense") && month.equals(current.month)){ //Expense
								dataset2[getMonth(current.month)] += current.monthlyvalue;
							}
							else {
								//do nothing
							}
							
							//monthvalue = getMonthValue(current,month,year);
							//yearvalue = getYearlyValue(current,month,year);
							
						}
					
			}
			
			
		}
		
		
		/**
		 * Will be implemented at a Later Date
		 * @param dp
		 * @param month
		 * @param y
		 * @return
		 */
    	public double getMonthValue(dbPackage dp,String month,String y) {
			return 0;
    	}
    	
    	/**
    	 * Will be implemented at a later date
    	 * @param dp
    	 * @param month
    	 * @param y
    	 * @return
    	 */
    	public double getYearlyValue(dbPackage dp,String month,String y) {
    		return 0;
    	}

    	/**
    	 * Return the numerical value for the month
    	 * @param name : String representation of the month
    	 * @return     : Integer value for the month
    	 */
		public int getMonth(String name) {
			if (name.equals("January")) {
				return 0;
			}
			else if (name.equals("February")) {
				return 1;
			}
			else if (name.equals("March")) {
				return 2;
			}
			else if (name.equals("April")) {
				return 3;
			}
			else if (name.equals("May")) {
				return 4;
			}
			else if (name.equals("June")) {
				return 5;
			}
			else if (name.equals("July")) {
				return 6;
			}
			else if (name.equals("August")) {
				return 7;
			}
			else if (name.equals("September")){
				return 8;
			}
			else if (name.equals("October")) {
				return 9;
			}
			else if (name.equals("November")) {
				return 10;
			}
			else {
				return 11;
			}
		}
		
		
		/**
		 * Create the graph as a view and return to the graph activity
		 * @param ctx  : The context for the graph activity
		 * @return     : The view containing the viewgraph
		 */
		public View getView(Context ctx) 
		{
			CategorySeries series;
			CategorySeries series2;
			if (time.equals("Monthly")) { //Monthly Graph
				//Incomes
				series = new CategorySeries("Income");
				for (int i = 0; i < dataset1.length; i++) {
				series.add("" + (i+1), dataset1[i]);
				}
			
				//Expenses
				series2 = new CategorySeries("Expense");
				for (int i = 0; i < dataset2.length; i++) {
				series2.add("" + (i+1), dataset2[i]);
				}
			}
			else { //Yearly graph
				String[] months = {"January","February","March","April","May","June","July","August","September","October","November","December"};
				//Incomes
				series = new CategorySeries("Income");
				for (int i = 0; i < dataset1.length; i++) {
				series.add(months[i], dataset1[i]);
				}
			
				//Expenses
				series2 = new CategorySeries("Expense");
				for (int i = 0; i < dataset2.length; i++) {
				series2.add(months[i], dataset2[i]);
			}
			}
			
			XYMultipleSeriesDataset msd = new XYMultipleSeriesDataset();
			msd.addSeries(series.toXYSeries());
			msd.addSeries(series2.toXYSeries());
			
			XYSeriesRenderer xysr = new XYSeriesRenderer();
			xysr.setDisplayChartValues(true);
			xysr.setChartValuesSpacing((float).5);
			xysr.setColor(Color.GREEN);
			
			XYSeriesRenderer xysr2 = new XYSeriesRenderer();
			xysr2.setDisplayChartValues(true);
			xysr2.setChartValuesSpacing((float)1);
			xysr2.setColor(Color.RED);
			
			XYMultipleSeriesRenderer xymsr = new XYMultipleSeriesRenderer();
			xymsr.addSeriesRenderer(xysr);
			xymsr.addSeriesRenderer(xysr2);
			xymsr.setChartTitle(time + " Budget Activity");
			if (time.equals("Monthly")) {
				xymsr.setXTitle("Day");
			}
			else {
				xymsr.setXTitle("Month");
			}
			xymsr.setYTitle("Value");
			
			View v = ChartFactory.getBarChartView(ctx, msd, xymsr, Type.DEFAULT);
			
			return v;
		}
	
}
